package v3.chapter2.synchronizedBlockLockAll; 

public class test1 {

	public static void main(String[] args) {
		// 同一个“对象监视器”
//		Service service=new Service();
//		MyObject object=new MyObject();
//		
//		ThreadA a=new ThreadA(service,object);
//		a.setName("a");
//		a.start();
//		
//		ThreadB b=new ThreadB(service,object);
//		b.setName("b");
//		b.start();
		
		// 非同一个“对象监视器”
		Service service=new Service();
		MyObject object1=new MyObject();
		MyObject object2=new MyObject();
		ThreadA a=new ThreadA(service,object1);
		a.setName("a");
		a.start();
		
		ThreadB b=new ThreadB(service,object2);
		b.setName("b");
		b.start();
		
	}

}


class ThreadA extends Thread { 
	private Service service;
	private MyObject object;

	public ThreadA(Service service,MyObject object) {
		super();
		this.service = service;
		this.object = object;
	}

	@Override
	public void run() {
		super.run(); 
		service.testMethod1(object);
	} 
}

class ThreadB extends Thread { 
	private Service service;
	private MyObject object;

	public ThreadB(Service service,MyObject object) {
		super();
		this.service = service;
		this.object = object;
	}

	@Override
	public void run() {
		super.run(); 
		service.testMethod1(object);
	} 
}

class Service{
	public void testMethod1(MyObject object) {
		synchronized (object) {
			try {
				System.out.println("testMehtod1_getLock time="+System.currentTimeMillis()+" run ThreadName="+Thread.currentThread().getName());
				Thread.sleep(2000);
				System.out.println("testMehtod1_releaseLock time="+System.currentTimeMillis()+" run ThreadName="+Thread.currentThread().getName());
			}catch(InterruptedException e) {
				e.printStackTrace();
			}
		}
	}
}

class MyObject{
	
}

